﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Sharp.Infrastructure.OAuth2.Model;

namespace Sharp.Infrastructure.OAuth2
{
    /// <summary>
    /// 授权基类
    /// </summary>
    public abstract class OAuth2Base
    {
        protected WebClient wc = new WebClient();
        public OAuth2Base()
        {
            wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.Headers.Add("Pragma", "no-cache");
        }

        #region 基础属性

        /// <summary>
        /// 返回的开放ID。
        /// </summary>
        public string openID = string.Empty;
        /// <summary>
        /// 访问的Token
        /// </summary>
        public string token = string.Empty;
        /// <summary>
        /// 过期时间
        /// </summary>
        public DateTime expiresTime;

        public AuthUserInfo UserEity = new AuthUserInfo();

        /// <summary>
        /// 首次请求时返回的Code
        /// </summary>
        internal string code = string.Empty;
        internal abstract OAuthServer server
        {
            get;
        }

        #endregion

        #region 非公开的请求路径和Logo图片地址。

        public abstract string OAuthUrl
        {
            get;
        }
        internal abstract string TokenUrl
        {
            get;
        }
        public abstract string ImgUrl
        {
            get;
        }

        #endregion

        #region 
        public string AppId { get; set; }
        public string AppKey { get; set; }
        public string CallbackUrl { get; set; }
        #endregion



        #region WebConfig对应的配置【AppKey、AppSercet、CallbackUrl】

        ///// <summary>
        ///// AppId
        ///// </summary>
        //public string AppId
        //{
        //    get
        //    {
        //        return Tool.GetConfig(server + ".AppId");
        //    }
        //}

        ///// <summary>
        ///// AppKey
        ///// </summary>
        //public string AppKey
        //{
        //    get
        //    {
        //        return Tool.GetConfig(server + ".AppKey");
        //    }
        //}
        ///// <summary>
        ///// CallbackUrl
        ///// </summary>
        //public string CallbackUrl
        //{
        //    get
        //    {
        //        return Tool.GetConfig(server + ".CallbackUrl");
        //    }
        //}

        #endregion

        #region 基础方法

        /// <summary>
        /// 获得Token
        /// </summary>
        /// <returns></returns>
        protected string GetToken(string method)
        {
            string result = string.Empty;
            try
            {
                string para = "grant_type=authorization_code&client_id=" + AppId + "&client_secret=" + AppKey + "&code=" + code + "&state=" + server;
                para += "&redirect_uri=" + System.Web.HttpUtility.UrlEncode(CallbackUrl) + "&rnd=" + DateTime.Now.Second;
                if (method == "POST")
                {
                    if (string.IsNullOrEmpty(wc.Headers["Content-Type"]))
                    {
                        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    }
                    result = wc.UploadString(TokenUrl, method, para);
                }
                else
                {
                    result = wc.DownloadString(TokenUrl + "?" + para);
                }

            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 获取是否通过授权。
        /// </summary>
        public abstract bool Authorize();

        #endregion
    }
    /// <summary>
    /// 提供授权的服务商
    /// </summary>
    public enum OAuthServer
    {
        /// <summary>
        /// 新浪微博
        /// </summary>
        SinaWeiBo,
        /// <summary>
        /// 腾讯QQ
        /// </summary>
        QQ,
        /// <summary>
        /// 
        /// </summary>
        None,
    }
}
